package org.dant.ant.extension.tasks;

import java.util.*;
import java.io.*;
import org.dant.ant.extension.tasks.commands.AntCallCommand;
import org.dant.ant.extension.tasks.internal.RemoteCommand;
import org.dant.ant.extension.types.AntParameter;
import org.dant.ant.extension.tasks.internal.Endpoint;

public class RemoteAntCall extends RemoteTask {

  private String buildFile;
  private String target;
  protected Map parameters = new HashMap();
  protected Vector params = new Vector();

  public RemoteAntCall() {
    rcmd = new AntCallCommand();
  }

  public void addParam(AntParameter param) {
    this.params.add(param);
  }

  public String getBuildFile() {
    return this.buildFile;
  }
  
  public void setBuildFile(String buildFile) {
    this.buildFile = buildFile;
  }
  
  public String getTarget() {
    return this.target;
  }
  
  public void setTarget(String target) {
    this.target = target;
  }

  public Map getParameters() {
    for (Iterator iter=params.iterator(); iter.hasNext();) {
      AntParameter param = (AntParameter) iter.next();
      parameters.put(param.getName(), param.getValue());
    }
    return this.parameters;
  }

  public void resolveDir() throws IOException {
    for (Iterator iter=destHosts.keySet().iterator(); iter.hasNext();) {
      Endpoint ep = (Endpoint) iter.next();
      RemoteCommand request = (RemoteCommand)destHosts.get(ep);
      File basedir = ep.getBaseDir();
      File buildfile = new File(buildFile);
      if (basedir == null || buildfile.isAbsolute()) continue;
      File absolutePath = fu.resolveFile(basedir, buildFile);
      request.appendParameters("buildfile", absolutePath.getCanonicalPath());
    }
  }
}
